package com.pet_service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet_service.entity.StaffSchedule;
import com.pet_service.mapper.StaffScheduleMapper;
import com.pet_service.result.Result;
import com.pet_service.service.StaffScheduleService;
import com.pet_service.vo.PageBean;
import com.pet_service.vo.PageResultVO;
import com.pet_service.vo.StaffScheduleVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 邹全伸
* @description 针对表【staff_schedule(服务人员排班表)】的数据库操作Service实现
* @createDate 2025-09-26 14:19:19
*/
@Service
public class StaffScheduleServiceImpl extends ServiceImpl<StaffScheduleMapper, StaffSchedule>
    implements StaffScheduleService {
    @Resource
    private StaffScheduleMapper staffScheduleMapper;

    // 分页查询
    @Override
    public PageResultVO<StaffScheduleVO> queryStaffSchedules(PageBean pageBean) {
        // 1. 查询当前页数据列表
        List<StaffScheduleVO> staffScheduleList = staffScheduleMapper.selectStaffSchedules(pageBean);

        // 2. 查询总记录数（用于计算总页数）
        Long total = staffScheduleMapper.selectStaffSchedulesCount(pageBean);

        // 3. 封装分页结果
        return new PageResultVO<>(total, staffScheduleList, pageBean);
    }


    // 查询排班详情
    @Override
    public StaffScheduleVO queryStaffScheduleById(Integer id) {
        return staffScheduleMapper.selectStaffScheduleById(id);
    }

    // 添加排班
    @Override
    @Transactional
    public Result addStaffSchedule(StaffScheduleVO staffScheduleVO) {
        // 1. 参数校验
        if (staffScheduleVO.getUserId() == null || staffScheduleVO.getScheduleDate() == null ||
                staffScheduleVO.getTimeSlot() == null || staffScheduleVO.getStatus() == null) {
            return Result.fail("必填字段不能为空");
        }

        // 2. 检查是否已存在相同排班
        if (staffScheduleMapper.existsByUserIdAndDateAndTimeSlot(staffScheduleVO.getUserId(),
                staffScheduleVO.getScheduleDate(), staffScheduleVO.getTimeSlot()) > 0) {
            return Result.fail("该服务人员在该时间段已有排班");
        }

        // 3. 创建排班实体
        var staffSchedule = new StaffSchedule();
        staffSchedule.setUser_id(staffScheduleVO.getUserId());
        staffSchedule.setSchedule_date(staffScheduleVO.getScheduleDate());
        staffSchedule.setTime_slot(staffScheduleVO.getTimeSlot());
        staffSchedule.setStatus(staffScheduleVO.getStatus());

        // 4. 保存排班
        int result = staffScheduleMapper.insert(staffSchedule);
        if (result > 0) {
            return Result.ok("排班添加成功");
        } else {
            return Result.fail("排班添加失败");
        }
    }

    // 更新排班
    @Override
    @Transactional
    public Result updateStaffSchedule(StaffScheduleVO staffScheduleVO) {
        // 1. 参数校验
        if (staffScheduleVO.getId() == null) {
            return Result.fail("排班ID不能为空");
        }
        if (staffScheduleVO.getUserId() == null || staffScheduleVO.getScheduleDate() == null ||
                staffScheduleVO.getTimeSlot() == null || staffScheduleVO.getStatus() == null) {
            return Result.fail("必填字段不能为空");
        }

        // 2. 检查排班是否存在
        if (!staffScheduleMapper.existsById(staffScheduleVO.getId())) {
            return Result.fail("排班记录不存在");
        }

        // 3. 检查是否已存在相同排班（排除自身）
        if (staffScheduleMapper.existsByUserIdAndDateAndTimeSlot(staffScheduleVO.getUserId(),
                staffScheduleVO.getScheduleDate(), staffScheduleVO.getTimeSlot()) > 0) {
            // 如果存在，需要确认是否是当前记录本身
            StaffScheduleVO existingSchedule = staffScheduleMapper.selectStaffScheduleById(staffScheduleVO.getId());
            if (existingSchedule != null) {
                boolean isSameSchedule = existingSchedule.getUserId().equals(staffScheduleVO.getUserId()) &&
                        existingSchedule.getScheduleDate().equals(staffScheduleVO.getScheduleDate()) &&
                        existingSchedule.getTimeSlot().equals(staffScheduleVO.getTimeSlot());
                if (!isSameSchedule) {
                    return Result.fail("该服务人员在该时间段已有排班");
                }
            }
        }

        // 4. 更新排班实体
        var staffSchedule = new StaffSchedule();
        staffSchedule.setId(staffScheduleVO.getId());
        staffSchedule.setUser_id(staffScheduleVO.getUserId());
        staffSchedule.setSchedule_date(staffScheduleVO.getScheduleDate());
        staffSchedule.setTime_slot(staffScheduleVO.getTimeSlot());
        staffSchedule.setStatus(staffScheduleVO.getStatus());

        // 5. 执行更新
        int result = staffScheduleMapper.updateById(staffSchedule);
        if (result > 0) {
            return Result.ok("排班更新成功");
        } else {
            return Result.fail("排班更新失败");
        }
    }

    // 删除排班
    // 修改 deleteStaffSchedule 方法
    @Override
    @Transactional
    public Result deleteStaffSchedule(Integer id) {
        // 1. 参数校验
        if (id == null) {
            return Result.fail("排班ID不能为空");
        }

        // 2. 检查排班是否存在
        if (!staffScheduleMapper.existsById(id)) {
            return Result.fail("排班记录不存在");
        }

        // 3. 物理删除排班记录
        int result = staffScheduleMapper.deleteStaffSchedulePhysically(id);
        if (result > 0) {
            return Result.ok("排班删除成功");
        } else {
            return Result.fail("排班删除失败");
        }
    }

}




